package edu.mse.camel.client.playback.handler;

import java.util.Date;
import java.util.Observable;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import edu.mse.camel.client.chat.message.Message;
import edu.mse.camel.client.eventbus.pubsub.ISubscriber;
import edu.mse.camel.client.events.IEvent;
import edu.mse.camel.client.events.peer.ReceiveChatEvent;
import edu.mse.camel.client.events.peer.ReceiveGenericPeerEvent;
import edu.mse.camel.client.events.server.JoinSessionEvent;
import edu.mse.camel.client.events.server.LeaveSessionEvent;
import edu.mse.camel.client.events.server.NotifySyncFinished;
import edu.mse.camel.client.playback.PlaybackManager;
import edu.mse.camel.client.playback.editors.PosterboardPlaybackEditor;
import edu.mse.camel.client.playback.events.ReceiveGenericPeerEventList;
import edu.mse.camel.client.playback.views.PlaybackMessageList;

public class ReceiveGenericPeerEventHandler extends Observable implements ISubscriber{

	private boolean isModelChange = false;
	private int changeIndex=0;
	
	@Override
	public void notifyEvent(IEvent e) {
		if (e instanceof ReceiveGenericPeerEvent) {
			ReceiveGenericPeerEvent genericEvent = (ReceiveGenericPeerEvent) e;
			isModelChange = true;
			ReceiveGenericPeerEventList.getInstance().addEvent(genericEvent, changeIndex);
		}
		else if (e instanceof ReceiveChatEvent){
			ReceiveChatEvent rmEvent = (ReceiveChatEvent) e;

			if (isModelChange == true)
			{
				changeIndex ++;
				Message message = new Message("Model Changed", null, rmEvent.getOrginalTime());
				PlaybackMessageList.getInstance().addMessage(message);
				isModelChange = false;
			}
			
			Message message = new Message(rmEvent.getContent(), rmEvent.getName(), rmEvent.getOrginalTime());
			PlaybackMessageList.getInstance().addMessage(message);

		}
		else if (e instanceof NotifySyncFinished){
			if (isModelChange == true)
			{
				Message message = new Message("Model Changed", null, new Date());
				PlaybackMessageList.getInstance().addMessage(message);
				isModelChange = false;
				this.setChanged();
				notifyObservers(message);
			}
			if(PlaybackManager.INSTANCE.isPlayback()){
				openPlaybackEditor();
			}
			changeIndex = 0;
		}
		else if (e instanceof JoinSessionEvent){
			PlaybackMessageList.getInstance().getMessages().clear();
			ReceiveGenericPeerEventList.getInstance().getEventList().clear();
			ReceiveGenericPeerEventList.getInstance().setCurrentIndex(-1);;
			isModelChange=false;
			changeIndex = 0;
			
			JoinSessionEvent jse = (JoinSessionEvent) e;
			PlaybackManager.INSTANCE.setPlayback(jse.isPlayback());
			
		} else if (e instanceof LeaveSessionEvent){
			if(PlaybackManager.INSTANCE.isPlayback())
				closePlaybackEditor();
			PlaybackManager.INSTANCE.setPlayback(false);
		}
		//else if (e instanceof LaunchPlayBackViewEvent) {
			
		//}
	}

	private void closePlaybackEditor() {
		Display.getDefault().asyncExec(new Runnable() {
			
			@Override
			public void run() {
				IWorkbenchPage page = 
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IEditorReference[] iEditorReferenceList = page.getEditorReferences();
				for(IEditorReference iEditorReference: iEditorReferenceList){
					if(iEditorReference.getName().equals(PlaybackManager.INSTANCE.getPosterboardFile()))
						page.closeEditor(iEditorReference.getEditor(false), true);
				}
				
			}
		});
	}

	private void openPlaybackEditor() {
		Display.getDefault().asyncExec(new Runnable() {
		
			@Override
			public void run() {
				IWorkbenchPage page = 
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IWorkspaceRoot r = ResourcesPlugin.getWorkspace().getRoot();
				IFile iFile = r.getProject(PlaybackManager.INSTANCE.getProjectName())
									.getFile(PlaybackManager.INSTANCE.getPosterboardFile());;

				try {
					
					IEditorReference[] iEditorReferenceList = page.getEditorReferences();
					for(IEditorReference iEditorReference: iEditorReferenceList){
						if(iEditorReference.getName().equals(PlaybackManager.INSTANCE.getPosterboardFile()))
							page.closeEditor(iEditorReference.getEditor(false), true);
					}

					IDE.openEditor(page, iFile, PosterboardPlaybackEditor.ID);
					
				} catch (PartInitException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
